import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute, Router } from "@angular/router";
import { Observable } from "rxjs/Observable";
import { SummaryAssistantService } from "summary-assistant";
import { TableOptions } from "quote";

import { DropdownModule } from "dropdown";
import { Subject } from "rxjs/Subject";
import { BASEURL } from "shared";

const filterOddsData =
  (dataArray: any[]) =>
    dataArray
      .reduce((acc, cur) =>
        cur.symbol && acc.symbolsArray.indexOf(cur.symbol) === -1
          ? {
            fullArray: [...acc.fullArray, cur],
            symbolsArray: [...acc.symbolsArray, cur.symbol]
          }
          : acc,
      { fullArray: [], symbolsArray: [] })
      .fullArray;

/**
 * QuoteTable的壳组件，给除板块类页面的QuoteTable传递参数
 * 
 * @export
 * @class QuoteindexComponent
 * @implements {OnInit}
 */
@Component({
  selector: 'quote-index',
  templateUrl: 'quote-index.component.html',
  // encapsulation: ViewEncapsulation.None,

  styleUrls: [
    'quote-index.component.less'
  ]
})
export class QuoteindexComponent implements OnInit {
  kConfig = {
    settingBtn: false, // 是否显示设置按钮
    scrollBar: false, // 是否显示底部滑动条
    title: false // 是否显示股票名称
  };
  tableHeaders = {
    headHeader: [
      {
        label: "序号",
        key: "$order",
        class: ["table-cell-order"]
      },
      {
        label: "名称",
        key: "prod_name",
        class: ["table-cell-name"]
      },
      // {
      //   label: "",
      //   key: "",
      //   appendModule: {
      //     module: DropdownModule,
      //     headerComponentName: "DropdownComponent",
      //     cellComponentName: "DropdownComponent",
      //   }
      // },
      {
        label: "股票代码",
        key: "symbol",
        class: ["table-cell-short"]
      }
    ],
    bodyHeader: [
      {
        label: "现价",
        key: "last_px",
        withUpOrDown: true,
      }, {
        label: "涨跌",
        key: "px_change",
        withUpOrDown: true,
      }, {
        label: "涨幅",
        key: "px_change_rate",
        withUpOrDown: true,
        withPercent: true
      },
      {
        label: "振幅",
        key: "amplitude",
        withPercent: true
      },
      {
        label: "成交量",
        key: "business_amount",
        withUnit: true,
      }, {
        label: "成交额",
        key: "business_balance",
        withUnit: true,
      }
    ]
  };
  commonHeaders = {
    headHeader: [
      {
        label: "序号",
        key: "$order",
        class: ["table-cell-order"]
      },
      {
        label: "名称",
        key: "name",
        class: ["table-cell-name"]
      },
      // {
      //   label: "",
      //   key: "",
      //   appendModule: {
      //     module: DropdownModule,
      //     headerComponentName: "DropdownComponent",
      //     cellComponentName: "DropdownComponent",
      //   }
      // },
      {
        label: "股票代码",
        key: "symbol",
        class: ["table-cell-short"]
      }
    ],
    bodyHeader: [
      {
        label: "现价",
        key: "current",
        withUpOrDown: true,
      }, {
        label: "涨跌",
        key: "change_amount",
        withUpOrDown: true,
      }, {
        label: "涨幅",
        key: "change_rate",
        withUpOrDown: true,
        withPercent: true
      },
      {
        label: "成交量",
        key: "volume",
        withUnit: true,
      }
    ]
  };

  tableOptions: TableOptions = {
    afterGetData: fullData => filterOddsData(fullData)
  };

  appendModule = {
    module: DropdownModule,
    headerComponentName: "DropdownComponent",
    cellComponentName: "DropdownComponent",
  };
  params;
  stockSource = new Subject();
  detailSource = this.stockSource.filter(stock => stock["symbol"]).switchMap(stock => this.summaryAssistant.getSummaryBySymbol(stock["symbol"]))
  placeSource = Observable.of('quote');


  tabList = [
    {
      text: "常用指数",
      url: "/common"
    },
    {
      text: "上证系列",
      url: "/ss"
    },
    {
      text: "深证系列",
      url: "/sz"
    },
    // {
    //   text: "中证系列",
    //   url: "/zz"
    // },
    // {
    //   text: "中金所",
    //   url: "/zj"
    // },
    // {
    //   text: "富时A50",
    //   url: "/a50"
    // },
  ]

  commonParams = {
    common: {
      id: "common",
      dataUrl: `${BASEURL}/stock/quotes/index`
    },
    ss: {
      id: "ss",
      dataUrl: `${BASEURL}/stock/quotes/market?type=5`
    },
    sz: {
      id: "sz",
      dataUrl: `${BASEURL}/stock/quotes/market?type=6`
    },
    // zz: {
    //   id: "zz",
    //   dataUrl: `${BASEURL}/stock/quotes/index`
    // },
    // zj: {
    //   id: "zj",
    // dataUrl: `${BASEURL}/stock/quotes/index`
    // },
    // a50: {
    //   id: "a50",
    // dataUrl: `${BASEURL}/stock/quotes/index`
    // },
  }

  constructor(
    private router: ActivatedRoute, 
    private goRouter: Router,
    private summaryAssistant: SummaryAssistantService
    ) { }

  ngOnInit() {
    this.params = this.router.params.map(params => {
      console.debug("OK: ", params, this.commonParams[(<any>params).id])
      return this.commonParams[(<any>params).id] || this.commonParams["index"];
    });
  }

  goCheckDetail(quote) {
    // console.debug("Quote Db Click: ", quote);
    this.goRouter.navigate([`/stock/${quote.symbol.replace(/\./, "_")}`]);
  }

  onQuoteClick(quote) {
    if (!quote && !quote.symbol) return;
    this.stockSource.next(quote);
  }

}